package com.mldong.modules.sys.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Dict;
import com.mldong.base.CommonPage;
import com.mldong.base.CommonResult;
import com.mldong.modules.sys.service.TimerService;
import com.mldong.timer.TimerModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author mldong
 * @since 2025-4-24
 */
@RestController
@Api(tags = "定时任务管理")
@RequiredArgsConstructor
public class TimerController {
    private final TimerService timerService;

    @PostMapping("/sys/timer/stop")
    @ApiOperation(value = "停止定时任务")
    @SaCheckPermission("sys:timer:stop")
    public CommonResult<?> stop(@RequestBody Dict param) {
        timerService.stop(param.get("id",param.getStr("name")));
        return CommonResult.ok();
    }
    @PostMapping("/sys/timer/start")
    @ApiOperation(value = "开始定时任务")
    @SaCheckPermission("sys:timer:stop")
    public CommonResult<?> start(@RequestBody Dict param) {
        timerService.start(param.get("id",param.getStr("name")));
        return CommonResult.ok();
    }
    @PostMapping("/sys/timer/update")
    @ApiOperation(value = "修改定时任务")
    @SaCheckPermission("sys:timer:update")
    public CommonResult<?> update(@RequestBody Dict param) {
        String id = param.get("id",param.getStr("name"));
        String remark = param.getStr("remark");
        String redisCron = param.getStr("redisCron");
        timerService.update(id,remark,redisCron);
        return CommonResult.ok();
    }
    @PostMapping("/sys/timer/detail")
    @ApiOperation(value = "定时任务详情")
    @SaCheckPermission("sys:timer:detail")
    public CommonResult<TimerModel> detail(@RequestBody Dict param) {
        return CommonResult.data(timerService.getById(param.get("id",param.getStr("name"))));
    }
    @PostMapping("/sys/timer/page")
    @ApiOperation(value = "分页查询定时任务列表")
    @SaCheckPermission("sys:timer:page")
    public CommonResult<CommonPage<TimerModel>> page(@RequestBody Dict param) {
        return CommonResult.data(timerService.page(param));
    }
    @PostMapping("/sys/timer/reset")
    @ApiOperation(value = "重置定时任务")
    @SaCheckPermission("sys:timer:reset")
    public CommonResult<?> reset(@RequestBody Dict param) {
        timerService.reset(CollectionUtil.newArrayList(Convert.toStrArray(param.get("ids"))));
        return CommonResult.ok();
    }
    @PostMapping("/sys/timer/executeImmediate")
    @ApiOperation(value = "立即执行")
    @SaCheckPermission("sys:timer:executeImmediate")
    public CommonResult<?> executeImmediate(@RequestBody Dict param) {
        timerService.executeImmediate(param.get("id",param.getStr("name")));
        return CommonResult.ok();
    }
}
